package com.example.azheng.rxjavamvpdemo;


import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.azheng.rxjavamvpdemo.base.BaseMvpActivity;
import com.example.azheng.rxjavamvpdemo.bean.BaseObjectBean;
import com.example.azheng.rxjavamvpdemo.bean.LoginBean;
import com.example.azheng.rxjavamvpdemo.contract.MainContract;
import com.example.azheng.rxjavamvpdemo.presenter.MainPresenter;
import com.example.azheng.rxjavamvpdemo.util.ProgressDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseMvpActivity<MainPresenter> implements MainContract.View {


    @BindView(R.id.lin_one)
    LinearLayout lintonOne;

    @BindView(R.id.iv_select_one)
    ImageView ivBg1;
    @BindView(R.id.iv_select_two)
    ImageView ivBg2;
    @BindView(R.id.iv_select_three)
    ImageView ivBg3;


    @BindView(R.id.lin_three)
    LinearLayout lintonThree;
    @BindView(R.id.lin_four)
    LinearLayout lintonFour;
    PagerAdapter adapter;

    @BindView(R.id.tv_main)
    TextView tvmain;

    private List<View> mViews = new ArrayList<>();
    @BindView(R.id.tv_my)
    TextView tvmy;

    @BindView(R.id.tv_set)
    TextView tvset;
    @BindView(R.id.viewpager)
    ViewPager mViewPager;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        mPresenter = new MainPresenter();
        mPresenter.attachView(this);
        ivBg1.setImageResource(R.drawable.tab_function_item_focus);
        tvmain.setTextColor(Color.RED);
        LayoutInflater inflater = LayoutInflater.from(this);
        View view1 = inflater.inflate(R.layout.fragment_chat, null);
        View view2 = inflater.inflate(R.layout.fragment_contact, null);
        View view3 = inflater.inflate(R.layout.fragment_my, null);
        mViews.add(view1);
        mViews.add(view2);
        mViews.add(view3);

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                resetImg();
                switch (position) {
                    case 0:
                        mViewPager.setCurrentItem(0);
                        tvmain.setTextColor(Color.RED);
                        ivBg1.setImageResource(R.drawable.tab_function_item_focus);

                        break;

                    case 1:
                        mViewPager.setCurrentItem(1);

                        ivBg2.setImageResource(R.drawable.tab_message_item_focus);
                        tvmy.setTextColor(Color.RED);
                        break;
                    case 2:
                        mViewPager.setCurrentItem(2);
                        tvset.setTextColor(Color.RED);
                        ivBg3.setImageResource(R.drawable.tab_contacts_item_focus);

                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        adapter = new PagerAdapter() {
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = mViews.get(position);

                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mViews.get(position));
            }

            @Override
            public int getCount() {
                return mViews.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        };
        mViewPager.setAdapter(adapter);
        mPresenter.login("POLICE_INDUSTRY_DICT");
    }


    @Override
    public void onSuccess(LoginBean  bean) {

        Toast.makeText(this, bean.getData().get(0).getVALUE(), Toast.LENGTH_SHORT).show();

    }

    @Override
    public void showLoading() {
        ProgressDialog.getInstance().show(this);
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.lin_one, R.id.lin_three, R.id.lin_four})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lin_one:
                tvmain.setTextColor(Color.RED);
                mViewPager.setCurrentItem(0);

                ivBg1.setBackgroundResource(R.drawable.tab_function_item_focus);

                break;

            case R.id.lin_three:
                mViewPager.setCurrentItem(1);
                tvmy.setTextColor(Color.RED);

                ivBg2.setBackgroundResource(R.drawable.tab_message_item_focus);
                break;
            case R.id.lin_four:
                mViewPager.setCurrentItem(2);
                tvset.setTextColor(Color.RED);
                ivBg3.setBackgroundResource(R.drawable.tab_contacts_item_focus);

                break;
        }


    }


    //重置TAB的字体颜色（也可以变化图片）
    private void resetImg() {
        tvmain.setTextColor(Color.BLACK);
        tvmy.setTextColor(Color.BLACK);
        tvset.setTextColor(Color.BLACK);
        ivBg1.setImageResource(R.drawable.tab_function_item_normal);
        ivBg2.setImageResource(R.drawable.tab_message_item_normal);
        ivBg3.setImageResource(R.drawable.tab_contacts_item_normal);
    }
}
